/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import entities.ViewPatientMaterial;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author viev
 */
public class PatientMaterialTableModel extends AbstractTableModel {

    private ArrayList<ViewPatientMaterial> list;
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/YYYY");

    public PatientMaterialTableModel() {
        list = new ArrayList<ViewPatientMaterial>();
    }

    public PatientMaterialTableModel(ArrayList<ViewPatientMaterial> list) {
        this.list = new ArrayList<ViewPatientMaterial>(list);
    }

    @Override
    public int getRowCount() {
        return list.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        ViewPatientMaterial record = list.get(rowIndex);
        Date startDate = record.getStartDate();
        Date endDate = record.getEndDate();
        switch (columnIndex) {
            case 0:
                return record.getPatientName();
            case 1:
                return record.getPatientPhone();
            case 2:
                if (startDate != null) {
                    return dateFormat.format(startDate);
                }
                return "";
            case 3:
                if (endDate != null) {
                    return dateFormat.format(endDate);
                }
                return "";
            case 4:
                return record.getReason();
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Name";
            case 1:
                return "Phone";
            case 2:
                return "Start Date";
            case 3:
                return "End Date";
            case 4:
                return "Reason";
            default:
                return "";
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }
    
    public boolean isEmpty() {
        return list.isEmpty();
    }

    public ViewPatientMaterial getAt(int index) {
        return list.get(index);
    }

    public void addRecord(ViewPatientMaterial record) {
        list.add(record);
        fireTableDataChanged();
    }

    public void updateRecord(ViewPatientMaterial record) {
        int index = list.indexOf(record);
        if (index != -1) {
            list.set(index, record);
            fireTableDataChanged();
        }
    }

    public void removeRecord(ViewPatientMaterial record) {
        list.remove(record);
        fireTableDataChanged();
    }

    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }
}
